<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>民工信息</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../assets/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../js/formSelects/formSelects-v4.css">
</head>

<body>
<fieldset class="layui-elem-field layui-field-title" style="padding-top: 5px;border: hidden">
    <form class="layui-form" action="" lay-filter="dataForm" autocomplete="off">
        <div class="layui-form-item layui-col-md11 layui-col-sm11">
            <label class="layui-form-label" ><span style="color:red">*</span>民工姓名</label>
            <div class="layui-input-block">
                <input type="text" name="name" id="name"  placeholder="民工姓名" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-col-md11 layui-col-sm11">
            <label class="layui-form-label"><span style="color:red">*</span>性别</label>
            <div class="layui-input-block">
                <select name="sex"  id="sex" lay-verify="required"   lay-search="">
                    <option value="" selected="selected">请选择</option>
                    <option value="0">男</option>
                    <option value="1">女</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item layui-col-md11 layui-col-sm11">
            <label class="layui-form-label"><span style="color:red">*</span>联系电话</label>
            <div class="layui-input-block">
                <input type="text" name="phone" id="phone"  placeholder="联系电话" lay-verify="required|phone" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text layui-col-md11 layui-col-sm11">
        <label class="layui-form-label"><span style="color:red">*</span>身份证号</label>
        <div class="layui-input-block">
            <input type="text" name="number" id="number" lay-verify="required|identity" placeholder="身份证号" class="layui-input">
        </div>
    </div>
        <div class="layui-form-item layui-form-text layui-col-md11 layui-col-sm11">
            <label class="layui-form-label"><span style="color:red">*</span>籍贯</label>
            <div class="layui-input-block">
                <input type="text" name="adress" id="adress" placeholder="籍贯" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-col-md11 layui-col-sm11">
            <div class="layui-input-block" style="text-align: center;">
                <button class="layui-btn" lay-submit="" id="btnSave" lay-filter="formSubmit">提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</fieldset>
<script src="../assets/layui/layui.js"></script>
<script>
    layui.config({
        base: '../js/'
    })
        .extend({
            formSelects: 'formSelects/formSelects-v4'
        });
    layui.use(['jquery','form','validate','common','formSelects','upload'], function () {
        var form = layui.form,
            $ = layui.jquery,
            layer = layui.layer,
            validate = layui.validate,
            formSelects = layui.formSelects,
            upload = layui.upload,
            common = layui.common;

        //监听提交
        form.on('submit(formSubmit)', function (data) {
            var name = $("#name").val();
            var sex = $("#sex").find("option:selected").val();
            var phone = $("#phone").val();
            var number = $("#number").val();
            var adress = $("#adress").val();

            if(name==""||sex==""||phone==""||number==""||adress=="")
            {
                common.error('请检查填写信息!');
                return false;
            }
            if(!phonenull(phone)){
                common.error("手机号格式不正确");
                return false;
            }
            if(number.length!=18){
                common.error("请输入正确的身份证号");
                return false;
            }
            data.field.name = name;
            data.field.sex = sex;
            data.field.phone = phone;
            data.field.number = number;
            data.field.adress= adress;
            $.ajax({
                type: 'post',
                url: '../project/addPerson',
                data: {json: JSON.stringify(data.field)},
                success: function(rtn){
                    if (rtn.code===0) {  //数据保存成功
                        var msg ="保存成功!";
                        common.showInfo(msg,function(){
                            //刷新前页
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.document.getElementById('btnSearch').click();
                            parent.layer.close(index);
                        });
                    }
                    else{
                        common.showError(rtn.msg);
                    }
                },
                error: function(XmlHttpRequest, textStatus, errorThrown){
                    common.showError('保存错误');
                }
            });
            return false;
        });
    });
    function phonenull(value) {
        if (!value) return true;
        if (value.length != 11 || !/^1\d{10}$$/.test(value)) {
            return false;
        }
        return true;
    }
</script>
</body>
</html>